package com.program.utils;

/**
 * @program: AirportSystem
 * @description: 链表实现的队列
 * @author: 19071027
 * @create: 2021-09-11 12:53
 **/
public class LinkQueue<T> implements Queue {

    private QueueNode head;

    private QueueNode tail;

    private int size;

    public LinkQueue() {
        this.size = 0;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        if (size == 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean add(Object o) {
        QueueNode node = new QueueNode(o);
        if (tail == null) {
            //队列为空
            head = node;
        }
        else {
            tail.next = node;
        }
        tail = node;
        size++;
        return true;
    }

    @Override
    public Object remove() {
        if (head != null) {
            QueueNode firstElement = head;
            head = head.next;
            size--;
            return firstElement.getElement();
        }
        return null;
    }

    @Override
    public Object peek() {
        if (head != null) {
            return head.getElement();
        }
        return null;
    }

}
